package org.scohen.juploadr.uploadapi.flickrrest.photosets;

import java.util.SortedMap;
import java.util.TreeMap;

import org.scohen.juploadr.app.PhotoSet;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.flickrrest.DefaultFlickrHandler;
import org.scohen.juploadr.uploadapi.flickrrest.FlickrMethod;
import org.scohen.juploadr.uploadapi.flickrrest.PhotoNotFoundException;
import org.scohen.juploadr.uploadapi.flickrrest.PostUploadAction;
import org.scohen.juploadr.uploadapi.flickrrest.RestFlickrApi;

public class CreatePhotoSet extends FlickrMethod implements PostUploadAction {
    private PhotoSet photoSet;

    public CreatePhotoSet(PhotoSet photoSet) {
        this.photoSet = photoSet;
    }

    public DefaultFlickrHandler getResponseHandler() {
        return new CreatePhotoSetResponseHandler(photoSet);
    }

    public SortedMap<String, String> getParams() {
        SortedMap<String, String> params = new TreeMap<String, String>();
        params.put("api_key", RestFlickrApi.FLICKR_API_KEY);
        params.put("method", "flickr.photosets.create");
        params.put("title", photoSet.getTitle());
        if (photoSet.getDescription() != null && photoSet.getDescription().length() > 0) {
            params.put("description", photoSet.getDescription());
        }
        params.put("primary_photo_id", photoSet.getPrimaryPhotoId());
        return params;
    }

    public boolean isAuthorized() {
        return true;
    }

    public String getErrorText() {
        return "create photoset " + photoSet.getTitle();
    }

    @Override
    protected ProtocolException buildProtocolException(int errorCode) {
        switch (errorCode) {
        case 1:
            return new NoTitleException();
        case 2:
            return new PhotoNotFoundException();
        case 3:
            return new CantCreateSetsException();
        default:
            return null;
        }
    }

}
